package com.authine.cloudpivot.ext.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 订单VO
 *
 * @author update张磊
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("订单VO")
@ToString(exclude = "orderGoodsVo")
public class OrderVo implements Serializable {

    private static final long serialVersionUID = 4785098776447339125L;
    @ApiModelProperty(value = "外部用户ID", required = true, example = "0000001", position = 1)
    private String clientUserId;
    @ApiModelProperty(value = "外部订单ID", example = "0000001", required = true, position = 2)
    private String clientOrderId;
    @ApiModelProperty(value = "ERP PO", example = "erp po", position = 7)
    private String erpPO;
    @ApiModelProperty(value = "税后商品运费共计", example = "20000.0", position = 7)
    private Double amount;
    @ApiModelProperty(value = "收货人", example = "收货人", required = true, position = 3)
    private String receiver;
    @ApiModelProperty(value = "电话", example = "13158585858", required = true, position = 4)
    private String phone;
    @ApiModelProperty(value = "地址", example = "上海市", required = true, position = 5)
    private String address;
    @ApiModelProperty(value = "订单商品VO", required = true, position = 6)
    private List<OrderGoodsVo> orderGoodsVo;
    @ApiModelProperty(value = "OS订单号", example = "000001", position = 7)
    private String clientOs;
    @ApiModelProperty(value = "实际下单人", example = "下单人", position = 7)
    private String actualOrder;

    @ApiModelProperty(value = "外部工厂代码", position = 7)
    private String clientFactoryCode;

    @ApiModelProperty(value = "扩展字段", position = 7, hidden = true)
    private String extendsAttr;


    public CustomerOrderVo toCustomerOrderVo() {

        CustomerOrderVo.CustomerOrderVoBuilder builder = CustomerOrderVo.builder();

        builder.client_user_id(clientUserId)
                .client_order_id(clientOrderId)
                .erp_po(erpPO)
                .amount(BigDecimal.valueOf(Optional.ofNullable(amount).orElse(0d)))
                .receiver(receiver)
                .phone(phone)
                .address(address)
                .client_os(clientOs)
                .actual_order(actualOrder)
                .clientFactoryCode(clientFactoryCode)
                .extends_attr(extendsAttr);

        if (!CollectionUtils.isEmpty(orderGoodsVo)) {

            builder.customerOrderGoodsVo(orderGoodsVo.stream().map(OrderGoodsVo::toCustomerOrderGoodsVo).collect(Collectors.toList()));

        }


        return builder.build();
    }

}
